package com.ftar.urgent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.ftar.urgent.R;

import java.util.List;

/**
 * @author: wei.jin
 * @date: 2023/7/10
 * @email: wei.jin@fanttec.com
 * @description：
 */
public class CatalogExpandListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> group;
    private List<List<String>> child;

    public CatalogExpandListAdapter(Context context, List<String> group,
                     List<List<String>> child) {
        this.context = context;
        this.group = group;
        this.child = child;
    }

    @Override
    public int getGroupCount() {
        return group.size();
    }

    //获取当前父item的数据
    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    //获取当前父item下的子item的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return child.size();
    }

    //得到子item需要关联的数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.get(childPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //得到子item的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }


    //hasStableIds的作用：指baseadapter的刷新顺序问题，即long getItemId(int position)boolean hasStableIds()getItemId；在调用 invalidateView()时，ListView会刷新显示内容。如果内容的id是有效的，系统会跟据id来确定当前显示哪条内容，也就是firstVisibleChild的位置。而id是否有效通过hasStableIds()确定。
    @Override
    public boolean hasStableIds() {
        return false;
    }


    //显示：group ,设置父item组件
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.catalog_expand_item, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView
                    .findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(group.get(groupPosition));
        holder.textView.setTextSize(25);
        holder.textView.setPadding(36, 10, 0, 10);
        return convertView;

    }

    //显示：child  ,设置子item的组件
    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.catalog_expand_item, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView
                    .findViewById(R.id.textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText(child.get(groupPosition).get(childPosition));
        holder.textView.setTextSize(20);
        holder.textView.setPadding(72, 10, 0, 10);
        return convertView;
    }


    //
    class ViewHolder {
        TextView textView;

    }

    //当选择子节点的时候,被调用 返回true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
